from selenium import webdriver
from selenium.webdriver.common.by import By
import Login
import time


def buy1(name,password,arg1,arg2,buyPsd,info2,test_png):

    driver = Login.login1(name,password)
    # 2 1
    driver.find_element(By.XPATH,f'//nav[@class="sidebar-main"]/ul/li[{arg1}]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH,f'//nav[@class="sidebar-main"]/ul/li[{arg1}]/ul/li[{arg2}]').click()
    time.sleep(2)
    driver.find_element(By.XPATH,'//div[@class="table-responsive"]/table/tbody/tr[1]/td[last()]/button').click()
    # 选择投资
    time.sleep(2)
    driver.find_element(By.XPATH,'//div[@class="layui-layer-content"]/input[@id="password"]').send_keys(buyPsd)
    driver.find_element(By.XPATH,"//div[@id='layui-layer1']/div[last()]/a[1]").click()
    time.sleep(2)
    result = driver.find_element(By.XPATH,'//div[@class="jconfirm-content"]/div').text

    if result == info2:
        print('通过')
    else:
        print('不通过！')
        driver.save_screenshot(f'{test_png}.png')

    assert result == info2

    driver.quit()


def buy2(name,password,arg1,arg2,buyPsd):

    driver = Login.login1(name,password)
    # 2 1
    driver.find_element(By.XPATH,f'//nav[@class="sidebar-main"]/ul/li[{arg1}]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH,f'//nav[@class="sidebar-main"]/ul/li[{arg1}]/ul/li[{arg2}]').click()
    time.sleep(2)
    driver.find_element(By.XPATH,'//div[@class="table-responsive"]/table/tbody/tr[1]/td[last()]/button').click()
    # 选择投资
    time.sleep(2)
    driver.find_element(By.XPATH,'//div[@class="layui-layer-content"]/input[@id="password"]').send_keys(buyPsd)
    driver.find_element(By.XPATH,"//div[@id='layui-layer1']/div[last()]/a[1]").click()
    time.sleep(2)

    return driver